package com.focusai.efairy.ui.activity.announce;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.focusai.efairy.R;
import com.focusai.efairy.model.project.ProjectItem;
import com.focusai.efairy.ui.base.SwipeBackBaseActivity;

import java.util.ArrayList;

/**
 * Created by luo_guangwen on 2017/10/23.
 */

public class AnnounceSeeRangeActivity extends SwipeBackBaseActivity implements View.OnClickListener {
    public final static String KEY_VISIBILITY_MODE = "key_visibility_mode";
    public final static String KEY_PROJECT_ITEMS = "key_project_items";
    private final int REQUESTCODE_SEE = 0x100;
    private final int REQUESTCODE_NOSEE = 0x101;
    private TextView textPartSee;
    private TextView textPartNoSee;
    private ImageView rbnAllSee;
    private ImageView rbnPartSee;
    private ImageView rbnPartNoSee;
    private int visibilityMode = 1;
    private ArrayList<ProjectItem> partSeeProjectItems;
    private ArrayList<ProjectItem> partNoSeeProjectItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_who_can_see);
        initView();
        bindEvents();
        setDefaultValues();
        setSelectImg();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem1 = menu.add(MENU_GROUP_ID, MENU_ITEM_ADD, 0, R.string.sure).setTitle(R.string.sure);
        MenuItemCompat.setShowAsAction(menuItem1, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == MENU_ITEM_ADD) {
            Intent intent = new Intent();
            intent.putExtra(KEY_VISIBILITY_MODE, visibilityMode);
            if (visibilityMode == 2) {
                intent.putExtra(KEY_PROJECT_ITEMS, partSeeProjectItems);
            } else if (visibilityMode == 3) {
                intent.putExtra(KEY_PROJECT_ITEMS, partNoSeeProjectItems);
            }
            setResult(RESULT_OK, intent);
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void initView() {
        textPartSee = findView(R.id.text_part_see);
        textPartNoSee = findView(R.id.text_part_nosee);
        rbnAllSee = findView(R.id.rbn_all_see);
        rbnPartSee = findView(R.id.rbn_part_see);
        rbnPartNoSee = findView(R.id.rbn_part_nosee);
    }

    @Override
    protected void bindEvents() {
        findView(R.id.ll_all_see).setOnClickListener(this);
        findView(R.id.ll_part_see).setOnClickListener(this);
        findView(R.id.ll_part_nosee).setOnClickListener(this);

    }

    @Override
    protected void setDefaultValues() {
        partSeeProjectItems = new ArrayList<>();
        partNoSeeProjectItems = new ArrayList<>();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_all_see:
                visibilityMode = 1;
                setSelectImg();
                break;
            case R.id.ll_part_see:
                visibilityMode = 2;
                setSelectImg();
                Intent intent = new Intent(getContext(), SelectProjectActivity.class);
                intent.putExtra(SelectProjectActivity.KEY_PROJECT_ITEMS, partSeeProjectItems);
                startActivityForResult(intent, REQUESTCODE_SEE);
                break;
            case R.id.ll_part_nosee:
                visibilityMode = 3;
                setSelectImg();
                Intent intent1 = new Intent(getContext(), SelectProjectActivity.class);
                intent1.putExtra(SelectProjectActivity.KEY_PROJECT_ITEMS, partNoSeeProjectItems);
                startActivityForResult(intent1, REQUESTCODE_NOSEE);

                break;
            default:
                break;
        }
    }

    private void setSelectImg(){
        if (visibilityMode == 1){
            rbnAllSee.setImageResource(R.drawable.icon_selected);
            rbnPartSee.setImageResource(R.drawable.icon_no_select);
            rbnPartNoSee.setImageResource(R.drawable.icon_no_select);
        }else if (visibilityMode == 2){
            rbnAllSee.setImageResource(R.drawable.icon_no_select);
            rbnPartSee.setImageResource(R.drawable.icon_selected);
            rbnPartNoSee.setImageResource(R.drawable.icon_no_select);
        }else if (visibilityMode == 3){
            rbnAllSee.setImageResource(R.drawable.icon_no_select);
            rbnPartSee.setImageResource(R.drawable.icon_no_select);
            rbnPartNoSee.setImageResource(R.drawable.icon_selected);
        }
    }

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUESTCODE_SEE) {
                ArrayList<ProjectItem> projectItems = (ArrayList<ProjectItem>) data.getSerializableExtra(SelectProjectActivity.KEY_PROJECT_ITEMS);
                partSeeProjectItems.clear();
                if (projectItems != null) {
                    partSeeProjectItems.addAll(projectItems);
                }
                textPartSee.setText(getString(R.string.has_select_project, partSeeProjectItems.size()));
            } else if (requestCode == REQUESTCODE_NOSEE) {
                ArrayList<ProjectItem> projectItems = (ArrayList<ProjectItem>) data.getSerializableExtra(SelectProjectActivity.KEY_PROJECT_ITEMS);
                partNoSeeProjectItems.clear();
                if (projectItems != null) {
                    partNoSeeProjectItems.addAll(projectItems);
                }
                textPartNoSee.setText(getString(R.string.has_select_project, partNoSeeProjectItems.size()));
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
